package com.chengxiaoxiao.dao.impl;

import com.chengxiaoxiao.dao.NoticeDao;
import com.chengxiaoxiao.domain.Notice;
import com.chengxiaoxiao.utils.db.*;

import java.util.List;

public class NoticeDaoImpl implements NoticeDao {
    private DBAssist db = new DBAssist(DBCPUtil.getDataSource());
    @Override
    public void add(Notice notice) {
        db.update("insert into notice(title,descript) values(?,?)",notice.getTitle(),notice.getDescript());
    }

    @Override
    public void update(Notice notice) {
        db.update("update notice set title=?,descript=? where id=?",notice.getTitle(),notice.getDescript(),notice.getId());

    }

    @Override
    public void delete(long id) {
        db.update("delete from notice where id=?",id);
    }

    @Override
    public List get() {
        return (List) db.query("select * from notice",new BeanListHandler(Notice.class));
    }

    @Override
    public Notice getbyId(long id) {
        return (Notice) db.query("select * from notice where id=?",new BeanHandler(Notice.class),id);
    }

    @Override
    public List getListPage(int startIndex, int pageSize) {
        String sql = "select * from notice limit ?,?";
        return (List) db.query(sql,new BeanListHandler(Notice.class),startIndex,pageSize);
    }

    @Override
    public int getTotalRecords() {
        return (int) db.query("select count(*) from notice",new ScalarHandler());
    }
}
